﻿#region

using System;
using System.ComponentModel;
using System.Windows.Forms;

#endregion

namespace Y2M.Controls
{
    [DefaultEvent("Paged")]
    public partial class DataPager : UserControl
    {
        public DataPager()
        {
            InitializeComponent();
        }

        private int _rowsPerPage;
        private int _totalCount;
        private int _page = 1;
        private int _maximum = 0;
        private int _minimum = 1;

        [Browsable(false)]
        public int StartRowIndex
        {
            get { return (_page - 1) * _rowsPerPage; }
        }

        private void ShowProgress()
        {
            txtProgress.Text = string.Format("{0} / {1}", _page, _maximum);

            btnFirst.Enabled = _page != _minimum;
            btnPrevious.Enabled = _page != _minimum;
            btnNext.Enabled = _page != _maximum;
            btnLast.Enabled = _page != _maximum;

            OnPaged(_page, StartRowIndex, _rowsPerPage);
        }

        public void Initialize(int totalCount, int rowsPerPage)
        {
            _rowsPerPage = rowsPerPage;

            _page = 1;

            _totalCount = totalCount;

            int maximum = _totalCount / _rowsPerPage;
            if (_totalCount % _rowsPerPage > 0)
                maximum++;

            _maximum = maximum;

            txtProgress.Text = string.Format("{0} / {1}", _page, _maximum);
        }

        #region button event handlers

        private void btnFirst_Click(object sender, EventArgs e)
        {
            _page = _minimum;
            ShowProgress();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            _page--;
            ShowProgress();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            _page++;
            ShowProgress();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            _page = _maximum;
            ShowProgress();
        }

        #endregion

        private void txeJump_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
                return;

            if (txeJump.Text.Length == 0)
                return;

            int value;
            bool parsed = int.TryParse(txeJump.Text, out value);

            txeJump.Text = string.Empty;

            if (parsed == false || value < 1 || value > _maximum)
            {
                MessageBox.Show(string.Format("1 부터 {0} 까지의 숫자만 입력할 수 있습니다.", _maximum));
                return;
            }

            _page = value;
            ShowProgress();
        }

        #region Paged event things for C# 3.0

        public event EventHandler<PagedEventArgs> Paged;

        protected virtual void OnPaged(PagedEventArgs e)
        {
            if (Paged != null)
                Paged(this, e);
        }

        protected virtual void OnPaged(int pageIndex, int startRowIndex, int maximumRows)
        {
            if (Paged != null)
                Paged(this, new PagedEventArgs(pageIndex, startRowIndex, maximumRows));
        }

        public class PagedEventArgs : EventArgs
        {
            public int PageIndex { get; set; }
            public int StartRowIndex { get; set; }
            public int MaximumRows { get; set; }

            public PagedEventArgs()
            {
            }

            public PagedEventArgs(int pageIndex, int startRowIndex, int maximumRows)
            {
                PageIndex = pageIndex;
                StartRowIndex = startRowIndex;
                MaximumRows = maximumRows;
            }
        }

        #endregion
    }
}